package com.kuun.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kuun.config.MongoPortfolioConfig;
import com.kuun.domain.Menu;
import com.kuun.mongo.repository.MenuRepository;
import com.kuun.service.MenuService;
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuRepository menuRepository ;
	
	@Autowired
	private MongoPortfolioConfig mongoPortfolioConfig ;

	@Override
	public boolean addMenu(Menu menu) {
		// TODO Auto-generated method stub
		if(menu!=null){
			menuRepository.save(menu) ;
			return true ;
		}else{
			return false ;
		}
	}

	@Override
	public List<String> findAllDisheName(String restaurantName) {
		// TODO Auto-generated method stub
		List<Menu> lm = findMenuByRestaurantName(restaurantName) ;
		List<String> ls = new ArrayList<String>() ;
		for(int i=0;i<lm.size();i++){
			ls.add(lm.get(i).getDisheName()) ;
		}
		return ls ;
	}

	@Override
	public List<Menu> findMenuByRestaurantName(String restaurantName) {
		// TODO Auto-generated method stub
		return menuRepository.findByRestaurantName(restaurantName) ;
	}

	@Override
	public List<String> findAllRestaurantName() {
		// TODO Auto-generated method stub
		List<Menu> lm = menuRepository.findAll() ;
		List<String> ls = new ArrayList<String>() ;
		for(int i=0;i<lm.size();i++){
			ls.add(lm.get(i).getRestaurantName()) ;
		}
		return ls ;
	}

	@Override
	public Menu findByRestaurantNameAndDisheName(String restaurantName, String disheName) {
		// TODO Auto-generated method stub
		return menuRepository.findByRestaurantNameAndDisheName(restaurantName,disheName) ;
	}

	@Override
	public List<Menu> getInfoByDisheName(String disheName) {
		// TODO Auto-generated method stub
		return menuRepository.getInfoByDisheName(disheName) ;
	}

	@Override
	public boolean updateDishePriceByRestaurantNameAndDisheName(String restaurantName, String disheName,
			double dishePrice) {
		// TODO Auto-generated method stub
		List<String> ls = findAllDisheName(restaurantName) ;
		boolean isSuccess = false ;
		if(disheName!=null && ls.contains(disheName)){
			try {
				MongoTemplate mongoTemplate = mongoPortfolioConfig.mongoTemplate();
				mongoTemplate.upsert(new Query(Criteria.where("disheName").is(disheName)), new Update().set("dishePrice", dishePrice), Menu.class);
				isSuccess = true ;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}	
//		Menu menu = findByRestaurantNameAndDisheName(restaurantName,disheName) ;
//		menu.setDishePrice(dishePrice);
//		if(menu!=null){
//			menuRepository.save(menu) ;
//			isSuccess = true ;
//		}
		return isSuccess ;
	}

	@Override
	public boolean deleteMenuByRestaurantNameAndDisheName(String restaurantName, String disheName) {
		// TODO Auto-generated method stub
		boolean isSuccess = false ;
		List<String> ls = findAllDisheName(restaurantName) ;
		if(ls.contains(disheName)){
			try {
				MongoTemplate mongoTemplate = mongoPortfolioConfig.mongoTemplate();
				mongoTemplate.remove(new Query(Criteria.where("disheName").is(disheName)), Menu.class) ;
				isSuccess = true ;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
//		Menu menu = findByRestaurantNameAndDisheName(restaurantName, disheName) ;
//		if(menu!=null){
//			menuRepository.delete(menu);
//			isSuccess = true ;
//		}
		return isSuccess ;
	}

	@Override
	public boolean deleteMenuByRestaurantName(String restaurantName) {
		// TODO Auto-generated method stub
		List<String> ls = findAllRestaurantName() ;
		boolean isSuccess = false ;
		if(ls.contains(restaurantName)){
			try {
				MongoTemplate mongoTemplate = mongoPortfolioConfig.mongoTemplate();
				mongoTemplate.remove(new Query(Criteria.where("restaurantName").is(restaurantName)), Menu.class) ;
				isSuccess = true ;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}		
//		List<Menu> menus = findMenuByRestaurantName(restaurantName) ;
//		if(menus!=null){
//			menuRepository.delete(menus);
//			isSuccess = true ;
//		}	
		return isSuccess ;
	}

	@Override
	public Page<Menu> pagingMenu(Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Menu> pmenu = menuRepository.findAll(pageable) ;
		return pmenu ;
	}
	
	
	
	
	
}
